package cn.itcast.jx.action.stat;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.dao.common.SqlDao;
import cn.itcast.jx.util.file.FileUtil;

public class StatChartAction  extends BaseAction{
	
	private SqlDao sqlDao;
	private String forward;
	
	public String getForward() {
		return forward;
	}


	public void setForward(String forward) {
		this.forward = forward;
	}


	public void setSqlDao(SqlDao sqlDao) {
		this.sqlDao = sqlDao;
	}


	public String factorySale() throws Exception {
		forward="factorysale";
		
		String sql = "select factory_name,sum(cnumber) from contract_product_c group by factory_name";
		List<String> list = sqlDao.executeSQL(sql);
		
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version='1.0' encoding='UTF-8'?>");
		sb.append("<pie>");
		
		for(int i = 0;i < list.size();i++) {
			sb.append("<slice title='" + list.get(i) + "'>" + list.get(++i) + "</slice>");
		}
		
		sb.append("</pie>");
		
		/**
		 * <?xml version="1.0" encoding="UTF-8"?>
			<pie>
			  <slice title="Twice a day" pull_out="true">358</slice>
			  <slice title="Once a day">258</slice>
			  <slice title="Once a week">154</slice>
			  <slice title="Never" url="http://www.interactivemaps.org" description="Click on the slice to find more information" alpha="50">114</slice>
			</pie>
		 * 
		 * **/
		
		FileUtil fileUtil = new FileUtil();
		String path = ServletActionContext.getRequest().getRealPath("/") + "stat/chart/factorysale";
		
		fileUtil.createTxt(path, "data.xml",sb.toString(), "utf-8");
		
		push(forward);
		return "factorySale";
	}
	
	public String productSale() throws Exception {
		forward = "productsale";
		String sql = "select b.* from(select product_no,sum(cnumber) as sumnum from contract_product_c group by product_no order by sumnum desc )b where rownum<16";
		
		List<String> list = sqlDao.executeSQL(sql);
		
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version='1.0' encoding='UTF-8'?>");
		sb.append("<chart>");
		
		sb.append("<series>");
		
		for(int i=0;i<list.size();i+=2) {
			sb.append("	<value xid='"+i+"'>"+list.get(i)+"</value>");
			
		}
		sb.append("</series>");
		
		sb.append("<graphs>");
		sb.append("<graph gid='30' color='#FFCC00' gradient_fill_colors='#111111, #1A897C'>");

		for(int i = 1; i < list.size();i+=2) {
			sb.append("<value xid='"+(i-1)+"' description='' url=''>"+ list.get(i)+"</value>");
			
		}

		sb.append("<graph>");
		sb.append("</graphs>");
		
		sb.append("</chart>");
		
		/**
		 * <?xml version="1.0" encoding="UTF-8"?>
<chart>
	<series>
		<value xid="0">西安局(347)</value>
		
		<value xid="24">陕西电力物业管理有限公司(25)</value>
	</series>
	<graphs>
		<graph gid="30" color="#FFCC00" gradient_fill_colors="#111111, #1A897C">
			<value xid="0" description="" url="">3483</value>

			<value xid="22" description="" url="">217</value>
			<value xid="23" description="" url="">238</value>
			<value xid="24" description="" url="">403</value>
		</graph>
	</graphs>
</chart>
		 * */
		
		String path = ServletActionContext.getRequest().getRealPath("/")+ "stat/chart/productsale";
		
		FileUtil fileUtil = new FileUtil();
		
		fileUtil.createTxt(path, "data.xml", sb.toString(), "utf-8");
		
		push(forward);
		return "productSale";
	}
	
	public String productSaleForJson() throws Exception {
		String sql = "select b.* from(select product_no,sum(cnumber) as sumnum from contract_product_c group by product_no order by sumnum desc )b where rownum<16";
		
		List<String> list = sqlDao.executeSQL(sql);
		
		StringBuffer sb = new StringBuffer();
		String[] color = {"#FF0F00","#FF6600","#FF9E01","#FCD202","#F8FF01","#B0DE09","#04D215","#0D8ECF","#0D52D1","#2A0CD0","#8A0CCF","#CD0D74","#754DEB","#DDDDDD","#999999","#333333","#000000"};
		sb.append("[");
		for(int i = 0,j = 1,z = 1;i<list.size();i+=2,j+=2,z++) {
			sb.append("{");
			sb.append("'product_no':");
			sb.append("'" + list.get(i)+"',");
			sb.append("'sale_num':");
			sb.append(list.get(j) + ",");
			sb.append("'color':");
			sb.append("'"+color[z]+"'");
			sb.append("},");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		
		put("charData", sb.toString());
		/**
		 * [
                {
                    "country": "USA",
                    "visits": 4025,
                    "color": "#FF0F00"
                },
                {
                    "country": "China",
                    "visits": 1882,
                    "color": "#FF6600"
                },
                {
                    "country": "Japan",
                    "visits": 1809,
                    "color": "#FF9E01"
                },
                {
                    "country": "Germany",
                    "visits": 1322,
                    "color": "#FCD202"
                }
            ]
		 * */
		
		return "productSaleForJson";
	}
	public String onlineInfo() throws Exception {
		String sql = "select a.a1,nvl(b.b1,0) from online_info_t a  "
				+ "left join (select to_char(login_time,'HH24') a1 ,count(*) b1 "
				+ "from login_log_p group by to_char(login_time,'HH24'))b "
				+ "on a.a1 = b.a1 order by a.a1 asc";

		
		List<String> list = sqlDao.executeSQL(sql);
		
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		
		for(int i = 0;i < list.size();i++) {
			sb.append("{");
			sb.append("'hour':");
			sb.append("'"+ list.get(i)+"',");
			sb.append("'value':");
			sb.append(list.get(++i));
			sb.append("},");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		
		/**
		 * [
                {
                    "year": "1950",
                    "value": -0.307
                }
           ]
		 * */
		put("charData",sb.toString());
		return "onlineInfo";
	}
}
